﻿//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 文件名: SysFieldDataSource.cs
// 作 者：代码自动生成
// 日 期：2012-2-21 14:17:24
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using Csla.Core;
using System.Data.SqlClient;
using iSun.DAL;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;

namespace iSun.Entity
{

    [Serializable]
    public partial class SysFieldItems : GBusinessListBase<SysFieldItems, SysFieldItem>
    {
           #region  Factory Methods

        private SysFieldItems()
        {
            /* Require use of factory methods */
            AllowNew = true;
            //AllowEdit = false;
        }


        internal static SysFieldItems GetChild(SysCustomDefineForm parent)
        {
            return DataPortal.FetchChild<SysFieldItems>(parent);
        }

        public static SysFieldItems NewChild()
        {
            return DataPortal.CreateChild<SysFieldItems>();
        }

        protected override object AddNewCore()
        {
            var item = SysFieldItem.NewChild();
            Add(item);
            return item;
        }

        #endregion

        #region  Data Access

        private void Child_Fetch(SysCustomDefineForm parent)
        {

            RaiseListChangedEvents = false;

            using (var ctx = ConnectionManager<SqlConnection>.GetManager(iSun.Model.BaseEntity.ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("FormID").Equal(parent.FormID);
                foreach (SysFieldItem c in db.Select<SysFieldItem>(q))
                {
                    this.Add(SysFieldItem.GetChild(parent, c));
                }
            }

            RaiseListChangedEvents = true;
        }

        #endregion
    
    }
	/// <summary>
    /// 表字段数据源
    /// </summary>
	[Serializable]
    [Table(Name="SysFieldDataSource")]
    [iSun.Model.Table("SysFieldDataSource","ID")]
    public class SysFieldItem : GBusinessBase<SysFieldItem>
	{
        
        #region Business Methods 业务方法
       
		/// <summary>
        /// 自定义表单数据源ID
        /// </summary>
        private static PropertyInfo< Guid > IDProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< Guid >("ID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }
        
		/// <summary>
        /// 表单ID
        /// </summary>
        private static PropertyInfo< Guid > FormIDProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< Guid >("FormID"));
        [Column]
        public Guid FormID
        {
            get { return GetProperty(FormIDProperty); }
            set { SetProperty(FormIDProperty, value); }
        }
		/// <summary>
        /// 表名
        /// </summary>
        private static PropertyInfo< string > TableNameProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("TableName"));
        [Column]
        public string TableName
        {
            get { return GetProperty(TableNameProperty); }
            set { SetProperty(TableNameProperty, value); }
        }
		/// <summary>
        /// 名称
        /// </summary>
        private static PropertyInfo< string > CaptionProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("Caption"));
        [Column]
        public string Caption
        {
            get { return GetProperty(CaptionProperty); }
            set { SetProperty(CaptionProperty, value); }
        }
		/// <summary>
        /// 字段名
        /// </summary>
        private static PropertyInfo< string > FieldNameProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("FieldName"));
        [Column]
        public string FieldName
        {
            get { return GetProperty(FieldNameProperty); }
            set { SetProperty(FieldNameProperty, value); }
        }
		/// <summary>
        /// 默认值
        /// </summary>
        private static PropertyInfo< string > DefualtValueProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("DefualtValue"));
        [Column]
        public string DefualtValue
        {
            get { return GetProperty(DefualtValueProperty); }
            set { SetProperty(DefualtValueProperty, value); }
        }
		/// <summary>
        /// 数据源类型[SON/SQL/FixData(固定数据,例如是(Y)/否(N))/XML/TEXT 暂时支持数据源为:JSON,SQL,固定数据]
        /// </summary>
        private static PropertyInfo< string > DataSourceTypeProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("DataSourceType"));
        [Column]
        public string DataSourceType
        {
            get { return GetProperty(DataSourceTypeProperty); }
            set { SetProperty(DataSourceTypeProperty, value); }
        }
		/// <summary>
        /// 数据源
        /// </summary>
        private static PropertyInfo< string > DataSourceProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< string >("DataSource"));
        [Column]
        public string DataSource
        {
            get { return GetProperty(DataSourceProperty); }
            set { SetProperty(DataSourceProperty, value); }
        }
		/// <summary>
        /// 是否必须输入
        /// </summary>
        private static PropertyInfo< int > IsRequireProperty = RegisterProperty(typeof(SysFieldItem),new PropertyInfo< int >("IsRequire"));
        [Column]
        public int IsRequire
        {
            get { return GetProperty(IsRequireProperty); }
            set { SetProperty(IsRequireProperty, value); }
        }
        [NotUndoable()]
        private SysCustomDefineForm form;
        public SysCustomDefineForm Form
        {
            get { return form; }
            set { form = value; }
        }
        /// <summary>
        /// 控件类型
        /// </summary>
        private static PropertyInfo<string> ControlsTypeProperty = RegisterProperty(typeof(SysFieldItem), new PropertyInfo<string>("ControlsType"));
        [Column]
        public string ControlsType
        {
            get { return GetProperty(ControlsTypeProperty); }
            set { SetProperty(ControlsTypeProperty, value); }
        }
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            //ValidationRules.AddRule(CommonRules.StringRequired, "TableName");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("TableName", 100));
            //ValidationRules.AddRule(CommonRules.StringRequired, "Caption");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Caption", 100));
            //ValidationRules.AddRule(CommonRules.StringRequired, "FieldName");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("FieldName", 100));
            //ValidationRules.AddRule(CommonRules.StringRequired, "DefualtValue");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DefualtValue", 100));
            //ValidationRules.AddRule(CommonRules.StringRequired, "DataSourceType");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DataSourceType", 20));
            //ValidationRules.AddRule(CommonRules.StringRequired, "DataSource");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DataSource", 500));
            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public SysFieldItem(){}

        internal static SysFieldItem NewChild()
        {
            return DataPortal.CreateChild<SysFieldItem>();
        }
        internal static SysFieldItem GetChild(SysCustomDefineForm xForm, SysFieldItem c)
        {
            c.Form = xForm;
            return DataPortal.FetchChild<SysFieldItem>(c);
        }

        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            LoadProperty(IDProperty, Guid.NewGuid());
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void Child_Fetch(SysFieldItem data)
        {
             LoadProperty(IDProperty, data.ReadProperty(IDProperty));
             LoadProperty(FormIDProperty, data.ReadProperty(FormIDProperty));
             LoadProperty(TableNameProperty, data.ReadProperty(TableNameProperty));
             LoadProperty(CaptionProperty, data.ReadProperty(CaptionProperty));
             LoadProperty(IDProperty, data.ReadProperty(IDProperty));
             LoadProperty(FieldNameProperty, data.ReadProperty(FieldNameProperty));
             LoadProperty(IDProperty, data.ReadProperty(IDProperty));
             LoadProperty(DefualtValueProperty, data.ReadProperty(DefualtValueProperty));
             LoadProperty(DataSourceTypeProperty, data.ReadProperty(DataSourceTypeProperty));
             LoadProperty(DataSourceProperty, data.ReadProperty(DataSourceProperty));
             LoadProperty(IsRequireProperty, data.ReadProperty(IsRequireProperty));
             LoadProperty(ControlsTypeProperty, data.ReadProperty(ControlsTypeProperty));

             Form = data.Form;
        }
        private void Child_Insert(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(iSun.Model.BaseEntity.ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Insert(this);
            }
        }
        private void Child_Update(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(iSun.Model.BaseEntity.ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Update(this);
            }
        }


        private void Child_DeleteSelf(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(iSun.Model.BaseEntity.ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Delete(this);
            }
        }

        #endregion

	}

  
}

